package com.linefly.online_car.Custom;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.bigkoo.pickerview.TimePickerView;
import com.bumptech.glide.Glide;
import com.linefly.online_car.Choose_Logo;
import com.linefly.online_car.R;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import Util.DateUtil;
import Util.MearsureUtil;
import Util.ToastUtil;


/**
 * Created by liheng on 2018/11/16.
 */

public class Custom_Dialog_shebei extends Dialog {

    private Context context;
    private TextView tv_sousuo,tv_shijian;
    EditText et_shebei,et_chepai,et_lxr,et_hm;
    String shebei,chepai,lianxiren,haoma,shijian,url;
    public ImageView imageView_tu;
    private Context dialog_context;

    public Custom_Dialog_shebei(Context context1,String shebei,String chepai, String lianxiren,
                                String haoma , String shijian,String url){

        super(context1, R.style.pagerDialog);
        context = context1;
        this.shebei = shebei;
        this.chepai = chepai;
        this.lianxiren = lianxiren;
        this.haoma = haoma;
        this.shijian = shijian;
        this.url = url;


    }

    public interface OnClickListener{
        void onClick(String shebei, String chepai,String lianxiren,String haoma,String shijian);
    }
    OnClickListener onClickListener;
    public void setOnClickListener(OnClickListener onClickListener){
        this.onClickListener = onClickListener;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().getAttributes().gravity = Gravity.CENTER;
        setContentView(R.layout.item_dialog_shebei);




        //按空白处不能取消动画
        setCanceledOnTouchOutside(true);
        setCancelable(true);

        Window dialogWindow = getWindow();
                 WindowManager.LayoutParams lp = dialogWindow.getAttributes();
                 DisplayMetrics d = context.getResources().getDisplayMetrics(); // 获取屏幕宽、高用

                 lp.width = (int) (d.widthPixels * 0.9); // 宽度度设置为屏幕的0.5
                 lp.height = MearsureUtil.dip2px(320);//(int) (d.heightPixels * 0.7); // 高度设置为宽度的0.8

                 dialogWindow.setAttributes(lp);

        et_chepai = (EditText) findViewById(R.id.chepai);
        et_shebei = (EditText) findViewById(R.id.shebei);
        et_lxr = (EditText) findViewById(R.id.lianxiren);
        et_hm = (EditText) findViewById(R.id.haoma);
        tv_shijian = (TextView)findViewById(R.id.shijian);
        tv_sousuo = (TextView)findViewById(R.id.sousuo);
        imageView_tu = (ImageView)findViewById(R.id.tupian1);
        et_chepai.setText(chepai);
        et_shebei.setText(shebei);
        et_lxr.setText(lianxiren);
        et_hm.setText(haoma);
        if(url.length()>0) {
            Glide.with(context).load(url).into(imageView_tu);
        }

        imageView_tu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //点击获取图片
                context.startActivity(new Intent(context, Choose_Logo.class));
            }
        });

        tv_shijian.setText(DateUtil.getDateToString((long)Integer.valueOf(shijian),"yyyy-MM-dd HH:mm"));

        tv_shijian.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                TimePickerView pvTime = new TimePickerView.Builder(context, new TimePickerView.OnTimeSelectListener() {
                    @Override
                    public void onTimeSelect(Date date, View v) {//选中事件回调
                        long time_now = DateUtil.getCurTimeLong();

                            DateFormat df1 = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                            tv_shijian.setText(df1.format(date));

                             shijian = String.valueOf(date.getTime()/1000);
                    }
                })
                        .setRangDate(Calendar.getInstance(),null)//起始终止年月日设定
                        .setType(new boolean[]{true, true, true, true, true, false})// 默认全部显示
                        .isDialog(true)
                        //      .setLabel("年","月","日","时","分","秒")//默认设���为年月日时分秒
                        .build();
                pvTime.show();


            }




        });



        tv_sousuo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(et_shebei.getText().toString().length() == 0){
                    ToastUtil.toast(context,"请填写设备号");
                    return;
                }else if(et_shebei.getText().toString().length() < 5){
                    ToastUtil.toast(context,"设备号不得小于5位");
                    return;
                }

                if(shijian.length() < 0){
                    ToastUtil.toast(context,"请选择时间");
                    return;
                }

                onClickListener.onClick(et_shebei.getText().toString(),et_chepai.getText().toString()
                ,et_lxr.getText().toString(),et_hm.getText().toString(),shijian);

            }
        });


    }






}

